package com.preskool.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.preskool.dto.ActionResult;
import com.preskool.po.Expenses;
import com.preskool.po.PersonalFees;
import com.preskool.service.ExpensesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @param
 * @Author swj
 * @Date 2021/1/7
 * @version1.0
 */
//花费,增加费用控制层
@RestController
public class ExpensesController {
    //注入ExpensesService接口
    @Autowired(required = false)
    private ExpensesService expensesService;

    /**
     * 查询全部数据,进行分页
     * @return
     */
    @RequestMapping(value = "/findAllExp",method = RequestMethod.GET)
    public ActionResult findAllPaging(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "8") Integer pageSize){
        PageHelper.startPage(currentPage,pageSize);
        List<Expenses> all = expensesService.findAllPage(currentPage,pageSize);
        PageInfo<Expenses> pif = new PageInfo<Expenses>(all);
        System.out.println("每页显示的条数:"+pif.getPageSize());
        System.out.println("当前页码:"+pif.getPageNum());
        System.out.println("总记录数:"+pif.getTotal());
        System.out.println("总页数:"+pif.getPages());
        if(all!=null){
            return new ActionResult(200,"查询成功",all);
        }
        return new ActionResult(203,"查询失败",null);
    }

    /**
     * 根据id查询单条数据
     * @param extid
     * @return
     */
    @RequestMapping(value = "/findOne",method = RequestMethod.GET)
    public ActionResult findOneById(Integer extid){
        Expenses one = expensesService.findOne(extid);
        if(one!=null){
            return new ActionResult(200,"查询成功",one);
        }
        return new ActionResult(203,"查询失败",null);
    }

    /**
     * 修改数据
     * 更新数据库
     * @param expenses
     * @return
     */
    @RequestMapping(value = "/updateExp",method = RequestMethod.POST)
    public ActionResult updateExpenses(Expenses expenses){
        int update = expensesService.updateExp(expenses);
        if(update>-1){
            return new ActionResult(200,"修改成功",null);
        }
        return new ActionResult(203,"修改失败",null);
    }

    /**
     * 添加数据
     * 加费页面,前端输入数据
     * @param expenses
     * @return
     */
    @RequestMapping(value = "/addExp",method = RequestMethod.POST)
    public ActionResult addExpenses(Expenses expenses){
        int add = expensesService.addExpenses(expenses);
        if(add>-1){
            return new ActionResult(200,"数据插入成功",null);
        }
        return new ActionResult(203,"数据插入失败",null);
    }

    /**
     * 根据id删除数据
     * @param exid
     * @return
     */
    @RequestMapping(value = "/delExp",method = RequestMethod.GET)
    public ActionResult delExpenses(Integer exid){
        int del = expensesService.delExpenses(exid);
        if(del>-1){
            return new ActionResult(200,"删除成功",null);
        }
        return new ActionResult(203,"删除失败",null);
    }
}
